﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class FrmImportList : TAM_SPORT.FormBase
    {
        private ImportDao imDao;
        public FrmImportList()
        {
            InitializeComponent();
            cbxKind.SelectedIndex = 0;
            imDao = new ImportDao();
            loadGrid();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            loadGrid();
        }

        private void loadGrid()
        {
            DataTable table = imDao.getImport(dtFrom.Value, dtTo.Value, cbxKind.SelectedIndex);
            dtgResult.Rows.Clear();
            double allTotal = 0;
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    int i = dtgResult.Rows.Add();
                    dtgResult.Rows[i].Cells[0].Value = i + 1;
                    dtgResult.Rows[i].Cells[1].Value = DateTime.Parse(row1["IMPORT_DATE"].ToString()).ToString("dd/MM/yyyy");
                    dtgResult.Rows[i].Cells[2].Value = row1["FULL_NAME"].ToString();
                    dtgResult.Rows[i].Cells[3].Value = Convert.ToDouble(row1["IMPORT_VALUE"]).ToString("#,###.##");
                    allTotal += Convert.ToDouble(row1["IMPORT_VALUE"]);
                    dtgResult.Rows[i].Cells[4].Value = row1["KIND"].ToString();
                    dtgResult.Rows[i].Cells[7].Value = row1["ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgResult.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
            txtTotal.Text = allTotal.ToString("#,###.##");
        }

        private void dtgResult_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = 0;
            int.TryParse(dtgResult.Rows[e.RowIndex].Cells[7].Value.ToString(), out id);
            if (id > 0)
            {
                if (e.RowIndex >= 0 && e.ColumnIndex == 5)
                {
                    Import im = new Import(id, true);
                    im.ShowDialog();
                    im.Dispose();
                    loadGrid();
                }
                if (e.RowIndex >= 0 && e.ColumnIndex == 6)
                {
                    Confirm f = new Confirm("Bạn muốn xóa phiếu nhập hàng này");
                    f.ShowDialog();

                    if (f.IsAccept)
                    {
                        if (imDao.deleteImport(id, LoginDao.userID))
                        {
                            Result re = new Result("Xóa thành công");
                            re.ShowDialog();
                            re.Dispose();
                            loadGrid();
                        }
                        else
                        {
                            Error err = new Error("Xóa thất bại");
                            err.ShowDialog();
                            err.Dispose();
                        }
                    }
                }
            }
        }
    }
}
